<?php

header('Content-type: text/html; charset=utf-8');
error_reporting(0);
include_once 'DatabaseHelper.php';
require_once 'template.php';

//Start session
session_start();
if (!isset($_SESSION['SESS_MEMBER_ID']) || (trim($_SESSION['SESS_MEMBER_ID']) == '')) {

    die("Vui lòng đăng nhập để thực hiện chức năng này!! <br/>
        <a href='index.php'> Trở về trang chủ </a>");
}

//if (isset($_POST['delete_draft'])) {
//    DatabaseHelper::getInstance()->connect();
//
//    $new_category = htmlspecialchars(strip_tags($_POST['new_category']));
//    $insertQuery = "INSERT INTO category (category_id, category_name) 
//                    VALUES (NULL,'$new_category')";
//    $result = DatabaseHelper::getInstance()->query($insertQuery);
//    header("location: category.php");
//} elseif (isset($_POST['edit_draft'])) {

//    DatabaseHelper::getInstance()->connect();
//    $query = "SELECT * FROM category";
//    $result = DatabaseHelper::getInstance()->query($query);
//
//    $row_cate_html = "";
//
//    while ($row = mysql_fetch_array($result)) {
//
//        $id = $row['category_id'];
//        $cateName = $row['category_name'];
//
//        $num_post_query = "SELECT count(id)as num_post FROM post where category = $id";
//        $num_post_result = DatabaseHelper::getInstance()->query($num_post_query);
//
//        $num_post = 0;
//        while ($num_post_row = mysql_fetch_array($num_post_result)) {
//            $num_post = $num_post_row['num_post'];
//        }
//
//        if (isset($_POST["remove-$id"]) &&
//                $_POST["remove-$id"] == '1' &&
//                $num_post == 0) {
//
//            $delete_query = "DELETE FROM category where category_id = $id";
//            DatabaseHelper::getInstance()->query($delete_query);
//        } else {
//
//            if (isset($_POST["rename-$id"])) {
//                $rename_cate = htmlspecialchars(strip_tags($_POST["rename-$id"]));
//
//                if ($rename_cate != "") {
//                    $update_cateName_query = "UPDATE category SET category_name = '$rename_cate' where category_id = $id";
//                    echo $update_cateName_query;
//                    DatabaseHelper::getInstance()->query($update_cateName_query);
//                }
//            }
//        }
//    }
//
//    DatabaseHelper::getInstance()->close();
//
//
//    header("location: category.php");
//} else {

    DatabaseHelper::getInstance()->connect();
    $query = "SELECT * FROM post where isdraft = '1' order by timestamp desc";
    $result = DatabaseHelper::getInstance()->query($query);

    $row_draft_html = "";

    while ($row = mysql_fetch_array($result)) {

        $id = $row['id'];
        $title = $row['title'];
        $cate_id = $row['category'];

        $timestamp = $row['timestamp'];
        $dateTimeString = date('H:i:s d-m-Y', $timestamp);
        $cate_name = "";

        $query_cate = "SELECT category_name FROM category where category_id = $cate_id limit 1";
        $cate_result = DatabaseHelper::getInstance()->query($query_cate);
        while ($row_cate = mysql_fetch_array($cate_result)) {
            $cate_name = $row_cate['category_name'];
        }


        $form_draft = new Page('template/form-draft-tpl.php');
        $form_draft->replace_tags(array("id_draft" => $id));

        $row_draft_html .= "
    <tr>
    <td> <span style = 'color:blueviolet;'>$title </span></td>
    <td nowrap=''>$dateTimeString</td>
    <td><i>$cate_name</i></td>
    <td nowrap=''>" . $form_draft->page . "</td>
    </tr>            
";
    }
    
    DatabaseHelper::getInstance()->close();

    $header = new Page('template/header-tpl.php');
    $footer = new Page('template/footer-tpl.php');

    $table_draft = new Page('template/draft-table-tpl.php');
    $table_draft->replace_tags(array(
        "draft_rows" => $row_draft_html
    ));
    $mainpage = new Page('template/draft-tpl.php');

    $mainpage->replace_tags(array(
        "header" => $header->page,
        "footer" => $footer->page,
        "draft_table" => $table_draft->page
    ));

    $mainpage->output();
//}
?>
